#include <stdio.h>
#include <malloc.h>
//#include <linux/types.h>
//#include <linux/signal.h>
//#include <linux/unistd.h>
//#include <linux/sched.h>
#include "System.h"
#include "Includes/Thread.h"
//#include "Syscall.h"
#include <signal.h>
#include <sched.h>

/// <summary>
/// Creates a thread to
/// execute within the virtual address
/// space of the calling process.
/// </summary>
int CreateThread(ThreadPtr thread, int (*start)(void*), void *arg)
{
	if (start == NULL)
		return -1;

	if (thread == NULL)
		return -1;

	if ((thread->stack = malloc(THREAD_STACK)) == NULL)
		return -2;

	//clone(start, (char*)thread->stack + THREAD_STACK, CLONE_FS | CLONE_FILES | CLONE_VM, arg);

	if ((thread->id = Syscall4(__NR_clone, (int)start, (int)((char*)thread->stack + THREAD_STACK), CLONE_FS | CLONE_FILES | CLONE_VM, (int)arg)) == -1)
		return -3;

	return 0;
}

